package com.edmendst.xmall.controller;

import com.edmendst.xmall.dto.CartInfo;
import com.edmendst.xmall.dto.ChangeCart;
import com.edmendst.xmall.pojo.Cart;
import com.edmendst.xmall.service.CartService;
import com.edmendst.xmall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author mjz
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private UserService userService;

    //    //创建/添加购物车
//    String addCart(CartInfo cartInfo);
//    cartInfo.getUserId() == null || cartInfo.getProductId() == null
    @PostMapping
    public String addCart(@RequestBody CartInfo cartInfo) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        Integer userIdByUserName = userService.selectUserIdByUserName(username);
        cartInfo.setUserId(userIdByUserName);
        return cartService.addCart(cartInfo);
    }

    //
//    //转换选中购物车
//    String changeCart(CartInfo cartInfo);
    @PutMapping
    public String changeCart(@RequestBody ChangeCart changeCart) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        Integer userIdByUserName = userService.selectUserIdByUserName(username);
        CartInfo cartInfo = new CartInfo();
        cartInfo.setProductId(changeCart.getProductId());
        cartInfo.setUserId(userIdByUserName);
        cartInfo.setSelected(changeCart.getSelected());
        return cartService.changeCart(cartInfo);
    }

    //
//    //修改商品数量
//    String updateCart(CartInfo cartInfo);
//
    @PutMapping("/number")
    public String updateCart(@RequestBody ChangeCart changeCart) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        Integer userIdByUserName = userService.selectUserIdByUserName(username);
        CartInfo cartInfo = new CartInfo();
        cartInfo.setProductId(changeCart.getProductId());
        cartInfo.setUserId(userIdByUserName);
        cartInfo.setOpcode(changeCart.getOpcode());
        return cartService.updateCart(cartInfo);
    }

    //    //删除商品
//    String deleteCart(CartInfo cartInfo);
    @DeleteMapping("/{productId}")
    public String deleteCart( @PathVariable Integer productId) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        Integer userIdByUserName = userService.selectUserIdByUserName(username);
        CartInfo cartInfo = new CartInfo();
        cartInfo.setProductId(productId);
        cartInfo.setUserId(userIdByUserName);
        return cartService.deleteCart(cartInfo);
    }

    //    //获取购物车
//    List<Cart> getCart(String userName);
    @GetMapping
    public List<CartInfo> getCart() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        return cartService.getCart(username);
    }
}
